﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace HuiZe.education.Resource.Controls
{
    public class CheckList : ListBox
    {

        public CheckList()
        {
            SelectedCheckListItems = new ObservableCollection<CheckListItem>();
            SelectedCheckListItems.CollectionChanged += SelectedCheckListItems_CollectionChanged;
        }

        private void SelectedCheckListItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            RoutedEventArgs args = new RoutedEventArgs(SelectedItemChangedEvent, curCheckListItem);
            RaiseEvent(args);

           
            
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new CheckListItem();
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            (element as CheckListItem).CheckedChanged += CheckList_CheckedChanged;
            (element as CheckListItem).MouseUp += CheckList_MouseUp;
            base.PrepareContainerForItemOverride(element, item);
        }

        private void CheckList_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            CheckListItem checkListItem = sender as CheckListItem;
            checkListItem.IsChecked = checkListItem.IsSelected;
        }

        public ObservableCollection<CheckListItem> SelectedCheckListItems
        {
            get { return (ObservableCollection<CheckListItem>)GetValue(SelectedCheckListItemsProperty); }
            set { SetValue(SelectedCheckListItemsProperty, value); }
        }
        public static readonly DependencyProperty SelectedCheckListItemsProperty =
            DependencyProperty.Register("SelectedCheckListItems", typeof(ObservableCollection<CheckListItem>), typeof(CheckList),
                new FrameworkPropertyMetadata(new ObservableCollection<CheckListItem>(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnSelectedItemChanged)));

        private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CheckList obj = d as CheckList;
            if (obj != null)
                obj.OnSelectedItemChanged(curCheckListItem);
        }

        private void OnSelectedItemChanged(CheckListItem item)
        {
            RoutedEventArgs args = new RoutedEventArgs(SelectedItemChangedEvent, item);
            RaiseEvent(args);
        }

        public static readonly RoutedEvent SelectedItemChangedEvent = EventManager.RegisterRoutedEvent("SelectedItemChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CheckList));
        public event RoutedEventHandler SelectedItemChanged
        {
            add
            {
                AddHandler(SelectedItemChangedEvent, value);
            }
            remove
            {
                RemoveHandler(SelectedItemChangedEvent, value);
            }
        }

        private List<CheckListItem> checkListItems = new List<CheckListItem>();
        public static CheckListItem curCheckListItem;
        private void CheckList_CheckedChanged(object sender, RoutedEventArgs e)
        {
            var obj = Common.CommonMethods.FindVisualParent<MultiComboBox>(this);
            curCheckListItem = sender as CheckListItem;
            bool ischecked = curCheckListItem.IsChecked;
            CheckListItem temp= sender as CheckListItem; 
            if (ischecked == true)
            {
                checkListItems.Add(curCheckListItem);
                SelectedCheckListItems.Add(temp);
            }
            else
            {
                checkListItems.Remove(curCheckListItem);
                SelectedCheckListItems.Remove(temp);
            }
        }
    }
}

